<!--
 * @Author: AHANG
 * @Date: 2022-07-15 11:09:11
 * @LastEditors: AHANG
 * @LastEditTime: 2022-07-15 15:42:24
 * @Description: 
-->
<template>
  <div>
    <el-button-group class="btn">
      <el-button type="primary" @click="exportALL">导出全部为 XLSX</el-button>
      <el-button type="primary" @click="exportIN">导出当前为 XLSX</el-button>
    </el-button-group>
  </div>
</template>

<script>
import * as XLSX from "xlsx";
export default {
  props: ["allData", "tableData"],
  name: "exportExcle",
  data() {
    return {};
  },
  mounted() {
    // console.log(this.allData);
  },
  methods: {
    exportALL() {
      this.exportExcle("ALL");
    },
    exportIN() {
      this.exportExcle("IN");
    },
    exportExcle(val) {
      var workbook = XLSX.utils.book_new();
      var sheet1;
      var title;
      if (val == "ALL") {
        sheet1 = XLSX.utils.json_to_sheet(this.allData);
        title = "书籍信息.xlsx";
      }
      if (val == "IN") {
        sheet1 = XLSX.utils.json_to_sheet(this.tableData);
        title = "书籍信息(部分).xlsx";
      }
      XLSX.utils.book_append_sheet(workbook, sheet1, "sheet1"); //工作簿名称
      // XLSX.utils.sheet_add_json(sheet1,data2);//把已存在的sheet中数据替换成json数据
      //4.输出工作表,由文件名决定的输出格式
      XLSX.writeFile(workbook, title); // 保存的文件名
      this.isMessage("导出完成", "success");
    },
  },
};
</script>

<style scoped>
.btn{
  margin: 10px;
}
</style>